/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.news.sender.test;

import cn.ac.iie.di.ban.pload.PLoader;
import cn.ac.iie.di.ban.pload.Status;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

/**
 *
 * @author zwlin
 */
public class test {

    private static SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
    static String test1 = "{\"msg_type\":\"1\",\"total_pushed_num\":\"1\",\"record_list\":[{\"DOMAIN\":\"asdf\",\n"
            + "\"CHANNEL\":\"1\",\n"
            + "\"ORIGINURL\":\"d3d3LnRlc3QuY29t\",\n"
            + "\"AUTHOR\":\"adbijas=\",\n"
            + "\"TITLE\":\"5LiL5pif5pyf5Y67\",\n"
            + "\"TEXT\":\"5oiQ5Yqf\",\n"
            + "\"PUBLISHTIME\":\""
            + formatter.format(new Date())
            + "\",\n"
            + "\"PHOTOURLS\":[\"5oiQ5Yqf\",\"5oiQ5Yqf\",\"5oiQ5Yqf\"],\n"
            + "\"VIDEOURLS\":[\"5oiQ5Yqf\",\"5oiQ5Yqf\",\"5oiQ5Yqf\"],\n"
            + "\"COLUMN\":[\"adbija031\",\"adbija032\",\"adbija033\"],\n"
            + "\"SPECIALNAME\":\"\",\n"
            + "\"ISDEL\":\"1\",\n"
            + "\"DELTIME\":\""
            + formatter.format(new Date())
            + "\",\n"
            + "\"PVNUM\":\"132\",\n"
            + "\"UVNUM\":165,\n"
            + "\"PRAISEDNUM\":303\n"
            + "\n"
            + "}]}";
    static String test2 = "{\"msg_type\":\"2\",\"total_pushed_num\":\"1\",\"record_list\":[{\"DOMAIN\":\"www.test.com\",\n"
            + "\"ORIGINURL\":\"adsf\",\n"
            + "\"POSTCHANNEL\":\"1\",\n"
            + "\"POSTID\":\"eHh4eHh4dGVzdA==\",\n"
            + "\"FPOSTID\":\"eHh4eHh4dGVzdA==\",\n"
            + "\"POSTTIME\":\""
            + formatter.format(new Date())
            + "\",\n"
            + "\"POSTCONT\":\"6K6p5oiRQkLkuKTlj6U=\",\n"
            + "\"USERID\":\"cXFxcXE=\",\n"
            + "\"POSTIP\":\"192.168.11.95\",\n"
            + "\"POSTSTATUS\":\"1\",\n"
            + "\"POSTDELTIME\":\""
            + formatter.format(new Date())
            + "\"\n"
            + "\n"
            + "}]}";
    static String test3 = "{\"msg_type\":\"3\",\"total_pushed_num\":\"1\",\"record_list\":[{\n"
            + "\"DOMAIN\":\"www.test.com\",\n"
            + "\"CHANNEL\":\"0\",\n"
            + "\"ORIGINURL\":\"d3d3LnRlc3=Q\",\n"
            + "\"PVNUM\":1024,\n"
            + "\"UVNUM\":2038}\n"
            + "]\n"
            + "}";
    static String test4 = "{\"msg_type\":\"4\",\"total_pushed_num\":\"1\",\"record_list\":[{\"DOMAIN\":\"sssd\",\"POSTCHANNEL\":\"1\",\"POSTID\":\"eHh4eHh4dGVzdA==\",\"LIKENUM\":5}]}";

    static String test_user = "{\n"
            + "	\"msg_type\": \"5\",\n"
            + "	\"total_pushed_num\": \"1\",\n"
            + "	\"record_list\": [{\n"
            + "		\"DOMAIN\": \"www.test.com\",\n"
            + "		\"USERID\": \"cXFxcXE=\",\n"
            + "		\"USERNAME\": \"bidkspovusnteg5\",\n"
            + "		\"REGISTERTIME\": \""
            + formatter.format(new Date())
            + "\",\n"
            + "		\"HEADURL\": \"bidkspovusnteg5=\",\n"
            + "		\"EMAIL\": \"helloa@alibpaba.com\",\n"
            + "		\"USERPHONE\": \"8618610110100\",\n"
            + "		\"USERQQ\": \"1232165452\",\n"
            + "		\"USERSTATE\": \"2\",\n"
            + "		\"PROCESSINGTIME\": \""
            + formatter.format(new Date())
            + "\"\n"
            + "}"
            + "]\n"
            + "}";
    static String test6 = "{\"msg_type\":\"appstore.basicinfo\",\"total_pushed_num\":\"1\",\"record_list\":[{\n"
            + "    \"APPSTORENAME\": \"azxc\",\n"
            + "    \"APPSTOREID\": \"1111\",\n"
            + "    \"WECHATID\": \"wechat11\",\n"
            + "    \"COMPANY\": \"360\",\n"
            + "    \"APPSTORELOGO\": \"d3d3LmJhaWR1LmNvbQ0K\",\n"
            + "    \"APPSTOREINFO\": \"d3d3LmJhaWR1LmNvbQ0Kd3d3LmJhaWR1LmNvbQ0K\",\n"
            + "    \"PUBLISHTIME\": \"2017122513550302\",\n"
            + "    \"SNAPSHOTTIME\": \"2017122513550300\",\n"
            + "    \"POPULARAPPLIST\": [],\n"
            + "    \"NEWPOPULARAPPLIST\":[],\n"
            + "    \"HOTSEARCH\": [],\n"
            + "    \"ISDEL\": \"0\",\n"
            + "    \"DELTIME\": \"2017122513550300\"\n"
            + "}]}";
    static String test7 = "{\"msg_type\":\"appstore.appcategory\",\"total_pushed_num\":\"1\",\"record_list\":[{\n"
            + "    \"APPCATEGORYID\": \"MzYwQXBw\",\n"
            + "    \"APPSTOREID\": \"1111\",\n"
            + "    \"APPCATEGORY\": \"MzYwQXBw\",\n"
            + "    \"CATEGORYINFO\": \"d3d3LmJhaWR1LmNvbQ0K\",\n"
            + "    \"SNAPSHOTTIME\": \"2017122513550302\",\n"
            + "    \"BARDIR\": \"ssad\",\n"
            + "    \"APPNUM\": 1,\n"
            + "    \"ISDEL\": \"0\",\n"
            + "    \"DELTIME\": \"2017122513550300\"\n"
            + "}]}";
    static String test8 = "{\"msg_type\":\"appstore.app\",\"total_pushed_num\":\"1\",\"record_list\":[{\n"
            + "    \"APPID\": \"MzYwQXBw\",\n"
            + "    \"APPNAME\": \"vvva\",\n"
            + "    \"APPPNAME\": \"MzYwQXBw\",\n"
            + "    \"APPSTOREID\": \"d3d3LmJhaWR1LmNvbQ0K\",\n"
            + "    \"APPVERSION\": \"MS4zLjMNCg==\",\n"
            + "    \"APPCATEGORYID\": \"Ng==\",\n"
            + "    \"APPINFO\": \"d3d3LmJhaWR1LmNvbQ0K\",\n"
            + "    \"SNAPSHOTTIME\": \"2017122513550302\",\n"
            + "    \"APPUPDATETIME\": \"2017122513550302\",\n"
            + "    \"APPSIZE\": \"123\",\n"
            + "    \"APPSCORE\": 1,\n"
            + "    \"APPUERRNUM\": 12,\n"
            + "    \"APPOWNER\": \"d3d3LmJhaWR1LmNvbQ0K\",\n"
            + "    \"APPINSTALLREQUEST\": \"d3d3LmJhaWR1LmNvbQ0K\",\n"
            + "    \"APPSNAPSHOTURLS\": [],\n"
            + "    \"USERIDLIST\": [],\n"
            + "    \"USERNUM\": 2,\n"
            + "    \"APPLOGOURL\": \"d3d3LmJhaWR1LmNvbQ0K\",\n"
            + "    \"APPSTATE\": \"0\",\n"
            + "    \"APPHARMSTATE\": \"0\",\n"
            + "    \"APPHARMMSG\": \"\",\n"
            + "    \"APPIP\": \"123.125.71.38\",\n"
            + "    \"APPIPLOCAL\": \"Ng==\",\n"
            + "    \"APPDOWNLOADURL\": \"d3d3LmJhaWR1LmNvbQ0K\",\n"
            + "    \"ISDEL\": \"0\",\n"
            + "    \"DELTIME\": \"2017122513550300\"\n"
            + "}]}";
    static String test9 = "{\"msg_type\":\"appstore.appcomments\",\"total_pushed_num\":\"1\",\"record_list\":[{\n"
            + "    \"COMMENTID\": \"MzYwQXBw\",\n"
            + "    \"APPSTOREID\": \"d3d3LmJhaWR1LmNvbQ0K\",\n"
            + "    \"APPVERSION\": \"MS4zLjMNCg==\",\n"
            + "    \"APPID\": \"Ng==\",\n"
            + "    \"TEXT\": \"d3d3LmJhaWR1LmNvbQ0K\",\n"
            + "    \"PUBLISHTIME\": \"2017122513550302\",\n"
            + "    \"SNAPSHOTTIME\": \"2017122513550302\",\n"
            + "    \"POSTNUM\": \"123\",\n"
            + "    \"PRAISEDNUM\": 1,\n"
            + "    \"PUBLISHFLOOR\": 12,\n"
            + "    \"ISDEL\": \"0\",\n"
            + "    \"DELTIME\": \"2017122513550300\"\n"
            + "}]}";
    static String test10 = "{\"msg_type\":\"appstore.appcompany\",\"total_pushed_num\":\"1\",\"record_list\":[{\n"
            + "    \"COMPANYNAME\": \"MzYwQXBw\",\n"
            + "    \"APPSTOREID\": \"d3d3LmJhaWR1LmNvbQ0K\",\n"
            + "    \"APPVERSION\": \"MS4zLjMNCg==\",\n"
            + "    \"APPID\": \"Ng==\",\n"
            + "    \"COMPANYCERTIFICATETYPE\": \"\",\n"
            + "    \"COMPANYCERTIFICATEID\": \"d3d3LmJhaWR1LmNvbQ0K\",\n"
            + "    \"LEGALNAME\": \"\",\n"
            + "    \"COMPANYPHONENUM\": \"\",\n"
            + "    \"COMPANYPROVINCE\": \"Ng==\",\n"
            + "    \"COMPANYCITY\": \"Ng==\",\n"
            + "    \"COMPANYDISTRICT\": \"Ng==\",\n"
            + "    \"COMPANYDETAILADDRESS\": \"aiws\",\n"
            + "    \"PERSIONNAME\": \"ssss\",\n"
            + "    \"PERSIONCERTIFICATETYPE\": \"\",\n"
            + "    \"PERSIONCERTIFICATEID\": \"\",\n"
            + "    \"PERSIONPHONENUM\": \"\",\n"
            + "    \"PERSIONPROVINCE\": \"Ng==\",\n"
            + "    \"PERSIONCITY\": \"Ng==\",\n"
            + "    \"PERSIONDISTRICT\": \"Ng==\",\n"
            + "    \"PERSIONDETAILADDRESS\": \"Ng==\",\n"
            + "    \"CONTACTNAME\": \"Ng==\",\n"
            + "    \"CONTACTPHONE\": \"\",\n"
            + "    \"CONTACTIDCARD\": \"\",\n"
            + "    \"SNAPSHOTTIME\": \"2017122513550300\"\n"
            + "}]}";

    static private String pre = "{\n"
            + "	\"msg_type\": \"5\",\n"
            + "	\"total_pushed_num\": \"1\",\n"
            + "	\"record_list\": [{\n"
            + "		\"DOMAIN\": \"www.test.com\",\n"
            + "		\"USERID\": \"bidkspovusnteg5==\",\n"
            + "		\"USERNAME\": \"bidkspovusnteg5\",\n"
            + "		\"REGISTERTIME\": \"19990101165210\",\n"
            + "		\"HEADURL\": \"bidkspovusnteg5=\",\n"
            + "		\"EMAIL\": \"helloa@alibpaba.com\",\n"
            + "		\"USERPHONE\": \"8618610110100\",\n"
            + "		\"USERQQ\": \"1232165452\",\n"
            + "		\"USERSTATE\": \"2\",\n"
            + "		\"PROCESSINGTIME\": \"20010110121510\"\n"
            + "}";
    static private String mid = ",{"
            + "		\"DOMAIN\": \"www.test.com\",\n"
            + "		\"USERID\": \"bidkspovusnteg5==\",\n"
            + "		\"USERNAME\": \"bidkspovusnteg5\",\n"
            + "		\"REGISTERTIME\": \"19990101165210\",\n"
            + "		\"HEADURL\": \"bidkspovusnteg5=\",\n"
            + "		\"EMAIL\": \"helloa@alibaba.com\",\n"
            + "		\"USERPHONE\": \"8618610110100\",\n"
            + "		\"USERQQ\": \"1232165452\",\n"
            + "		\"USERSTATE\": \"2\",\n"
            + "		\"PROCESSINGTIME\": \"20010110121510\"\n"
            + "	}";
    static private String suffix = "]\n"
            + "}";
//
    static private String test = "{\"total_pushed_num\":\"1\",\"msg_type\":\"1\",\"record_list\":[{\"DOMAIN\":\"news.163.com\",\"PUBLISHTIME\":\"20171129010101\",\"CHANNEL\":\"0\",\"COLUMN\":[\"0001H321\"],\"SOURCE\":[\"新华社\"],\"TITLE\":\"5Lmg6L+R5bmz5oyH5Ye65paw5YeG5YiZ5paw5p2h5L6L55qE5pyA6bKc5piO54m554K5\",\"TEXT\":\"4oCc5Zug6ICM5b6q5LmL77yM5LiO6YGT56We5LmL44CC4oCd5Y6G5Y+y5piv546w5a6e55qE5qC55rqQ77yM5LyY6Imv5Lyg57uf44CB5Y6G5Y+y57uP6aqM5piv5pyA5aW955qE6JCl5YW75YmC77yM5Lmf5piv5pyA5aW955qE5pWZ56eR5Lmm44CC5paw5b2i5Yq/5LiL5Lil6IKD5YWa5YaF5pS/5rK755Sf5rS744CB5Yqg5by65YWa5YaF55uR552j77yM5LiN5piv6YeN5paw5byA5aeL77yM6ICM5piv5o6l57ut5YmN6L+b44CC5Lmg6L+R5bmz5oyH5Ye677yM5Zyo5Lik5Liq5paH5Lu25LitIOKAnOa3seWFpeaAu+e7k+S6huaI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by6\",\"ORIGINURL\":\"aHR0cDovL25ld3MuMTYzLmNvbS8xNi8xMTA3LzEzL0M1OTg2VkczMDAwMTg5RkguaHRtbA==\"}]}";

    public static void main(String[] args) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, Exception {
//        String str ="8b3f73728b71ca62caa7080376c196841";
//        System.out.println(str.substring(0, 32));
        //整个过程是线程安全的，可在多个线程同时使用。
        //获取发送工具。参数是企业唯一key

//        PLoader sender = PLoader.getInstance("059c41de8298e800deeff0ea035b3f63");
//        sender.setTest("127.0.0.1:10240");
//        PLoader sender = PLoader.getInstance("53ac6575c3dd1ee98c5f909ee61b8ae8");
        PLoader sender = PLoader.getInstance("d5c995b937e06455873bb0485453f308");
        try {
            //发送。参数是json格式的数据内容。
//			sender.send("data_json 0");
//			sender.send("data_json 1");
            //发送的结果
            Status status = null;
            Random rand = new Random();
            int a = 0;
            HashMap<Integer, String> midMap = new HashMap<>();
            int rom = 0;
            for (int ttmp = 0; ttmp < 1000; ++ttmp) {
                for (int i = 0; i < 1000; ++i) {

                    Thread.sleep(1000 + rand.nextInt(1000));
                    String json = pre;
                    int midLen = 230 + rand.nextInt(70);
                    json += getMidString(midMap, midLen);
                    for (int j = 0; j < rand.nextInt(70); ++j) {
                        json += mid;

                    }
                    json += suffix;
//                    status = sender.send(test1);//发送成功
//                    if (status.success) {
//                        if (true || ++a > 100) {
//                            System.out.println(new Date().toString() + "\t" + a + "\tsuccessful");
//                            a = 0;
//                        }
//                    } else {
//                        //错误说明
//                        System.out.println(status.reasonPhrase);
//                    }
//                    status = sender.send(test2);//发送成功
//                    if (status.success) {
//                        if (true || ++a > 100) {
//                            System.out.println(new Date().toString() + "\t" + a + "\tsuccessful");
//                            a = 0;
//                        }
//                    } else {
//                        //错误说明
//                        System.out.println(status.reasonPhrase);
//                    }
//                    status = sender.send(test3);//发送成功
//                    if (status.success) {
//                        if (true || ++a > 100) {
//                            System.out.println(new Date().toString() + "\t" + a + "\tsuccessful");
//                            a = 0;
//                        }
//                    } else {
//                        //错误说明
//                        System.out.println(status.reasonPhrase);
//                    }
//                    status = sender.send(test4);//发送成功
//                    if (status.success) {
//                        if (true || ++a > 100) {
//                            System.out.println(new Date().toString() + "\t" + a + "\tsuccessful");
//                            a = 0;
//                        }
//                    } else {
//                        //错误说明
//                        System.out.println(status.reasonPhrase);
//                    }
//                    String[] s = new String[]{test1};
//                    String[] s = new String[]{test1, test2, test3, test4, test_user};
                    String[] s = new String[]{test6, test7, test8, test9, test10};
                    if (rom >= 5) {
                        rom = 0;
                    }
                    status = sender.send(s[rom]);
                    rom++;

                    //发送成功
                    if (status.success) {
                        if (true || ++a > 100) {
                            System.out.println(new Date().toString() + "\t" + a + "\t " + rom + " successful");
                            a = 0;
                        }
                    } else {
                        //错误说明
                        System.out.println(status.reasonPhrase+ "\t" + a + "\t " + rom);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            //处理可能的报错。
        }
        //发送结束后，结束sender。
        //注意：相同的企业唯一key对应的发送工具是同一个实例，一旦close，需要重新start才能使用。
        sender.close();
        //可以不调用sender.close，在程序结束时调用closeAll，自动关闭所有发送工具。
        //PLoader.closeAll();

    }

    private static String getMidString(HashMap<Integer, String> map, int len) {
        if (map.containsKey(len)) {
            return map.get(len);
        } else if (len == 1) {
            map.put(1, mid);
            return mid;
        } else {
            String tmp = getMidString(map, len / 2);
            tmp += tmp;
            if (len % 2 == 0) {
            } else {
                tmp += mid;
            }
            map.put(len, tmp);
            return tmp;
        }
    }
}
